<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>请假单签字</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <tips optionTemplate="signLeave"></tips>
        <div class="signTitle" v-if="!signImg">请在签名区域签字确认</div>
        <div class="commentBody">
          <div class="esign_textarea" ref="esignTextarea" v-if="!signImg">
            <vue-esign ref="esign" :height="250" :width="widthTexyarea" :isCrop="isCrop" :lineWidth="lineWidth"
                       :lineColor="lineColor" :bgColor.sync="bgColor" v-if="widthTexyarea"/>
          </div>
          <div class="esign_textarea" ref="esignTextarea" v-else>
            <img :src="signImg" alt="">
          </div>
        </div>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="saveFn">确定保存</el-button>
        <el-button size="small" @click="handleReset" type="danger" plain>重新签字</el-button>
      </div>
    </div>
    <AuxiliaryModule @getInformation="getInformation" :typeData="typeData"></AuxiliaryModule>
  </div>
</template>

<script>
import {leaveSign} from '@/api/admin/student_leave'

export default {
  props: ['visible', 'leaveId'], // recruitmentType examine 审核
  components: {},
  data() {
    return {
      dialogVisible: false,
      typeData: 'leave_signature',
      signImg: '',
      widthTexyarea: 0,
      lineWidth: 7,
      lineColor: '#000000',
      bgColor: '#fff',
      isCrop: false,
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        let n = this;
        this.$nextTick(() => {
          n.widthTexyarea = this.$refs.esignTextarea.clientWidth;
        })
      }
    }
  },
  created() {
  },
  methods: {
    handleReset() {
      this.signImg = ''
      this.$refs.esign.reset()
    },
    // 同步数据
    getInformation(option) {
      console.log('同步啦：', option)
      for (let item of option) {
        if (item.type === 'sign') {
          this.signImg = item.value
        }
      }
    },
    saveFn() {
      let n = this
      if (!this.signImg) {
        this.$refs.esign.generate().then(res => {
          n.checkSignFn(res)
        }).catch(err => {
          n.$message.error('请签字确认')
        })
      } else {
        this.checkSignFn()
      }
    },
    checkSignFn(pic) {
      let n = this
      this.$confirm('确定请假单签字, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        leaveSign({ids: n.leaveId, pic: (pic ? pic : n.signImg)}).then(res => {
          console.log(res)
          if(!res.code){
            n.$emit('getInit')
          }else{
            n.$message.error('签字失败'+res.msg)
          }
        })
      }).catch(() => {
      });
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    }
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 650px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}


.jobTime span {
  padding: 0 10px;
}

.esign_textarea {
  border: 3px solid #bdbdbd;
}

.signTitle {
  text-align: center;
  color: #999;
}
</style>


